/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: deterministic_client.h
 *
 * Purpose:  The DeterministicClient class provides the functionality for an Application to
 *           run a cyclic deterministic execution, see 7.7.3. Each Process which needs support
 *           for cyclic deterministic execution has to instantiate this class
 *
 * Developer:
 *   wen.gu , 2020-04-20
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_EXEC_DETERMINISTIC_CLIENT_H__
#define __ARA_EXEC_DETERMINISTIC_CLIENT_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <cstdint>
#include <functional>
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace exec
{

/** Defines the return codes for WaitForNextActivation operations. Scoped Enumeration of uint8_t .*/
enum class ActivationReturnType : uint8_t
{
    kRegisterServices = 0, /** application shall register communication
                             * services(this must be the only occasion for
                             * performing service registering) */

    kServiceDiscovery= 1,  /** application shall do communication service
                             * discovery (this must be the only occasion for
                             * performing service discovery) */
    kInit= 2, /**application shall initialize its internal data structures(once) */
    kRun = 3,  /** application shall perform its normal operation */
    kTerminate = 4, /** application shall terminate */

};

/** Defines the return codes for "get activation timestamp" operations. Scoped Enumeration of uint8_t .*/
enum class ActivationTimeStampReturnType : uint8_t
{
    kSuccess = 0,
    kNotAvailable = 1
};
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class Worker
{
public:
    virtual ~Worker() = default;
public:
    virtual void Runable() = 0;
};

class DeterministicClient
{
public:
    using TimeStamp = uint64_t;
    using FuncRunable = std::function<void()>;
public:
    DeterministicClient() noexcept;
    ~DeterministicClient() noexcept;

public:
    ActivationReturnType WaitForNextActivation() const noexcept;

    template<typename Container>
    void RunWorkerPool(Worker& runnableObj, Container& container) const noexcept
    {

    }

    uint64_t GetRandom() const noexcept;
    ActivationTimeStampReturnType GetActivationTime(TimeStamp& ts) const noexcept;
    ActivationTimeStampReturnType GetNextActivationTime(TimeStamp& ts) const  noexcept;

};

} /** namespace exec */
} /** namespace ara */

#endif /** !__ARA_EXEC_DETERMINISTIC_CLIENT_H__ */

